package com.wushi.lenovo.asia5b.app_di.inbox.presenter;

import com.mvp.base.BasePresenter;
import com.mvp.base.NetParams;
import com.mvp.common.GlobalValue;
import com.mvp.common.LocalManageUtil;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_di.inbox.mvpview.InboxMsgFragmentView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import javax.inject.Inject;

/**
 * Created by zhangyuncai on 2019/1/8.
 */
public class InboxMsgFragmentPresenter extends BasePresenter<InboxMsgFragmentView> {

    ApiService apiService;
    public int page;//当前页数
    public int sumPage;//最后页数

    @Inject
    public InboxMsgFragmentPresenter(ApiService apiService) {
        this.apiService = apiService;
    }

    public void getMsgInbox() {
        baseSubscribe(apiService.getMsgInboxs(UserInfo.getMemberId(), LocalManageUtil.getAppLanguage())
                , resultModel -> {
                    getView().getMsgInboxResult(resultModel);
                });
    }

    public void getMsgOrders(int refresh) {
        if (!UserRepository.isLogin()) {
            return;
        }
        if (refresh <= 1) {
            page = 1;
            sumPage = 1000;
        }
        if (page > sumPage) {
            getView().dismissLoadingBar("");
            return;
        }
        MapHelper mapHelper = MapHelper.newHashMap();
        mapHelper.put("member_id", UserInfo.getMemberId());
        mapHelper.put("lang", LocalManageUtil.getAppLanguage());
        mapHelper.put("page_id", page);
        mapHelper.put("records", 10);
        baseSubscribe(apiService.getMsgOrders(mapHelper)
                , resultModel -> {
                    getView().getMsgOrdersResult(resultModel, page);
                    if (resultModel.isSuccess() && resultModel.getData() != null) {
                        page++;
                        sumPage = GlobalValue.getSumPage(GlobalValue.records, resultModel.getData().getTotal());
                    }
                });
    }

    public void readNotifacation(int notification_id, String notification_type) {
        baseSubscribe(apiService.readNotification(UserInfo.getMemberId(), notification_id, notification_type),
               new NetParams(false),
                resultModel -> {
                    getView().readNotifacationResult(resultModel, notification_id);
                });
    }

}